
import {
    getForexList, getForexById, addForex, exchange, deleteForex, approvalForex,
    queryPaymen, splitForex, updateForex, rollbackForex
} from '../../services/foreignExchange/foreignExchangeManage';
import Constants from '../../utils/Constants';
import {parse} from 'qs';
import { message } from 'antd';
const R = require('ramda');

export default {
    namespace: 'foreignExchangeManage',
    state: {
        page: {
            page: 1,
            size: 10,
            totalRow: 0,
            totalPage: 0
        },
        list:[],
        dataSource: {},
        foreign: {},
        status: "",
    },
    reducers: {

        //-----------------setList

        /**
         *
         * @param state
         * @param payload
         * @returns {{list, page: *}}
         */
        setlist(state, { payload }) {
            return { ...state, list: payload.list, page: payload.page, status: payload.status}
        },

        //--------------------setEntity


        setPaymen(state,{payload})
        {
          return {...state,paymen:payload.paymen}
        },
        /**
         * 设置产品
         * @param state
         * @param action
         * @returns {{dataSource: (string|state.dataSource|{}|*)}}
         */
        setDataSource(state, action) {
            let payload = action.payload;
            return {...state, dataSource: payload.data};
        },

    },
    effects: {

        /**
         * 获取外汇列表
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *getForexList({payload}, { call, put, select }) {
            let page = yield select(state => state.foreignExchangeManage.page);
            page = R.isNil(payload) || R.isNil(payload.page) ? page : payload.page;

            let status = yield select(state => state.foreignExchangeManage.status);
            status = R.isNil(payload) || R.isNil(payload.status) ? status : payload.status;

            const res = yield call(getForexList, {
                payman: R.isNil(payload) || R.isNil(payload.payman) ? "" : payload.payman,
                beginAmt: R.isNil(payload) || R.isNil(payload.beginAmt) ? "" : payload.beginAmt,
                endAmt: R.isNil(payload) || R.isNil(payload.endAmt) ? "" : payload.endAmt,
                contractno:R.isNil(payload)  || R.isNil(payload.contractno) ? "" : payload.contractno,
                beginTime: R.isNil(payload) || R.isNil(payload.beginTime) ? "" : payload.beginTime,
                endTime: R.isNil(payload) || R.isNil(payload.endTime) ? "" : payload.endTime,
                payaccount: R.isNil(payload) || R.isNil(payload.payaccount) ? "" : payload.payaccount,
                nameOrCode: R.isNil(payload) || R.isNil(payload.nameOrCode) ? "" : payload.nameOrCode,
                status: status,
                page
            });

            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                yield put({
                    type: 'setlist',
                    payload: {
                        list: R.isNil(content.list) ? [] : content.list,
                        page : R.isNil(content.page) ? Constants.DEFAULTPAGE : content.page,
                        status: status
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },


        /**
         * 获取付汇人
         */
        *queryPaymen({payload},{call,put,select})
        {
          const res=yield call(queryPaymen,{});


          if(res.data.resultCode==="ok")
          {
              let data = res.data.content;
              yield put({
                  type:"setPaymen",
                  payload:{
                      paymen:data
                  }
              })
          }

        },

        /**
         * 获取外汇详情
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *getForexById({payload}, {call, put, select}) {
            const res = yield call(getForexById, {id: payload.id});

            if (res.data.resultCode === "ok") {
                let data = res.data.content;
                yield put({
                    type: "setDataSource",
                    payload: {
                        data: data,
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }

        },

        /**
         * 删除外汇
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *deleteForexById({payload}, {call, put, select}) {
            let page = yield select(state => state.foreignExchangeManage.page);
            let status = yield select(state => state.foreignExchangeManage.status);

            const res = yield call(deleteForex, {id: payload.id});
            if (res.data.resultCode === "ok") {
                message.info("删除成功！");

                if(payload.onSuccess)payload.onSuccess();


            } else {
                message.error(res.data.errMessage);
            }

        },

        /**
         * 结汇
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *exchange({ payload }, { call, put, select }) {
            let page = yield select(state => state.foreignExchangeManage.page);
            let status = yield select(state => state.foreignExchangeManage.status);

            const res = yield call(exchange, payload);

            if (res.data.resultCode === "ok") {
                message.info("结汇成功！");
                if(payload.onSuccess)payload.onSuccess();

            } else {
                message.error(res.data.errMessage);
                if(payload.onFail)payload.onFail();
            }
        },

        /**
         * 外汇拆分
         *
         */
        *splitForex({payload},{call,put,select})
        {
          const res=yield call(splitForex,payload)
          if(res.data.resultCode=="ok"){

             if(payload.onSuccess)payload.onSuccess();
          }else{
              message.error(res.data.errMessage);
              if(payload.onFail)payload.onFail();
          }
        },


        /**
         * 外汇审核
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *approvalForex({ payload }, { call, put, select }) {
            let page = yield select(state => state.foreignExchangeManage.page);
            let status = yield select(state => state.foreignExchangeManage.status);

            const res = yield call(approvalForex, payload);
            console.info(payload);
            if (res.data.resultCode === "ok") {
                message.info("审核成功！");

                if(payload.onSuccess)payload.onSuccess();

            } else {
                message.error(res.data.errMessage);
            }
        },



        /**
         * 新增外汇
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *addForex({payload}, {call, put, select}) {
            let page = yield select(state => state.foreignExchangeManage.page);
            let status = yield select(state => state.foreignExchangeManage.status);

            const res = yield call(addForex, payload);
            if (res.data.resultCode === "ok") {
                message.info("增加成功！");

                if(payload.onSuccess)payload.onSuccess();


            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 更新外汇
         * @param payload
         * @param call
         * @param put
         * @param select
         */
         * updateForex({payload}, {call, put, select}) {

            const res = yield call(updateForex, {id: payload.id, exchangeurl: payload.exchangeurl});
            if (res.data.resultCode === "ok") {
                message.info("保存水单成功！");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                message.error(res.data.errMessage);
            }
        },


        /**
         * 外汇拆分
         *
         */
         *rollbackForex({payload},{ call }) {
            const res=yield call(rollbackForex, { ...R.omit(['onSuccess', 'onFail'])(payload) });

            if(res.data.resultCode=="ok"){
                message.info("外汇回退成功！");
                if(payload.onSuccess)payload.onSuccess();
            }else{
                message.error(res.data.errMessage);
                if(payload.onFail)payload.onFail();
            }
        },
    },
    subscriptions: {
        setup({ history, dispatch }) {

        }
    }
};